package com.attilax.cmd;

    import java.io.BufferedReader;  
import java.io.File;  
import java.io.FileWriter;  
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.List;
import java.io.UnsupportedEncodingException;
import java.io.InputStreamReader;  
import java.util.List;
import java.util.StringTokenizer;

import com.attilax.core;
import com.attilax.exception.ExUtil;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.attilax.lang.Closure2;
import com.attilax.task.TaskService;
import com.attilax.sms.NoDeviceEx;
import com.attilax.sms.pullEx;
    /** 
     * Java 锟斤拷锟�  CMD 锟斤拷锟斤拷锟较� 
     * @author MrChu 
     * 2013-2-19 
     */  
public class CmdX {

	public static void main(String[] args) {
	 
		String r = t2();
System.out.println("----echo:"+r);
System.out.println("--f");


	}
	private static String t2() {
		pathx.isWebPathMode=true;
		String bat=pathx.classPathParent()+"/0screenUtil.bat";
		bat=bat.replace("/", "\\");
	 
		String cmd="schtasks /create /tn atitask@id@ /tr \"'@cmd@'\" /sc MINUTE /mo 2  ".replace("@cmd@", bat).replace("@id@", filex.getUUidName());
		System.out.println(cmd);
		String r=exe(cmd);
		
		return r;
	}
	private static String t() {
		pathx.isWebPathMode=true;
String bat=pathx.classPathParent()+"/other/del_indexs.bat";
bat=bat.replace("/", "\\");
String cmd=" cmd.exe /c \""+bat+"\"";
System.out.println(cmd);
String r=exe(cmd);
		return r;
	}
@Deprecated
	public static void redirectEcho2Screen(Process process) {
		System.out.println("--echo stdin info:");
		echoCmdResult_out(process.getInputStream());
		System.out.println("--echo err info:");
		echoCmdResult_out(process.getErrorStream());
		
	}

	public static String exe_sync(String cmd) {
		String r = "";
		// 执行 CMD 命令
		Process process = null;
		try {
			process = Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();throw new RuntimeException(e);
		}
		r = echoCmdResult_asStr(process.getInputStream());
		String err = echoCmdResult_asStr(process.getErrorStream());
		r = r + "\r\n" + err;// echoCmdResult_asStr(process.getErrorStream());

		// System.out.println("cmd ext finish!");
		return r;
	}

	public static String exe(String cmd) {
		try {
			String r = "";
			// 执行 CMD 命令
			Process process = Runtime.getRuntime().exec(cmd);
		//	System.out.println("--echo stdin info:");
			
			TaskService taskService = new TaskService();
			System.out.println("--start get input stream"+filex.getUUidName());
			FutureTask async_task=taskService.async(()->{
				String echo1 = echoCmdResult_asStr(process.getInputStream());
				System.out.println("--echo1:"+echo1);
				return	echo1;
			},10);
			new Thread(async_task).start();
			System.out.println("--start get err stream"+filex.getUUidName());
			FutureTask async_task_err=taskService.async(()->{
				String echo2 = echoCmdResult_asStr(process.getErrorStream());
				System.out.println("--echo2:"+echo2);
				return	echo2;
			},10);
			new Thread(async_task_err).start();
		//	 new Thread().join();
			System.out.println("--start await std stream"+filex.getUUidName());
			r=taskService.<String>await(async_task, 15, "$output_timeout_ex");
			System.out.println("-- finish await std stream"+filex.getUUidName());
			System.out.println("----------------------------------------");
			System.out.println("--start await err stream"+filex.getUUidName());
			String err=taskService.<String>await(async_task_err,1, "$err_stream_timeout_ex");
			System.out.println("--finish await err stream"+filex.getUUidName());
//			r=taskService.async_await(()->{
//				return	echoCmdResult_asStr(process.getInputStream());
//			},10,"$output_timeout_ex");
			
	//		System.out.println("--echo err info:");
		
			r = r + "\r\n" +err;// echoCmdResult_asStr(process.getErrorStream());

			//System.out.println("cmd ext finish!");
			return r;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
	
	public static String exe(String cmd,Runnable rx) {
		try {
			String r = "";
			// 执行 CMD 命令
			Process process = Runtime.getRuntime().exec(cmd);
		//	System.out.println("--echo stdin info:");
			r = echoCmdResult_asStr(process.getInputStream());
	//		System.out.println("--echo err info:");
			r = r + "\r\n" + echoCmdResult_asStr(process.getErrorStream());

			//System.out.println("cmd ext finish!");
			return r;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
	
	private static void echoCmdResult_out(InputStream cmdStream,Closure2   lineEvent)

	{
		
		 
		 
		BufferedReader reader = null;
		// BufferedReader reader;
		InputStreamReader inputStreamReader;
		try {
			inputStreamReader = new InputStreamReader(cmdStream,
					"gbk");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		reader = new BufferedReader(inputStreamReader);
		String line = null;
		try {
			line = reader.readLine();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		while (line != null) {
			lineEvent.execute(line);
			try {
				line = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (reader != null) {
			try {
				reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	//	return reader;
	}
		private   String echoCmdResult_outV2(InputStream is)

	{
		
		 
		 
		BufferedReader reader = null;
		// BufferedReader reader;
		InputStreamReader inputStreamReader;
		try {
			inputStreamReader = new InputStreamReader(is,
					"gbk");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		reader = new BufferedReader(inputStreamReader);
		String line = "";
		try {
			line = reader.readLine();
		} catch (IOException e1) {
			ExUtil.throwExV2(e1);
		}

		while (line != null) {
			System.out.println(line);
			try {
				line = reader.readLine();
			} catch (IOException e) {
				ExUtil.throwExV2(e);
			}
		}
		if (reader != null) {
			try {
				reader.close();
			} catch (IOException e) {
				ExUtil.throwExV2(e);
			}
		}
		return line;
	}


	private static BufferedReader echoCmdResult_out(InputStream is)

	{
		
		 
		 
		BufferedReader reader = null;
		// BufferedReader reader;
		InputStreamReader inputStreamReader;
		try {
			inputStreamReader = new InputStreamReader(is,
					"gbk");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		reader = new BufferedReader(inputStreamReader);
		String line = null;
		try {
			line = reader.readLine();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		while (line != null) {
			System.out.println(line);
			try {
				line = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (reader != null) {
			try {
				reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return reader;
	}
	@Deprecated
		public static Process exec(String paramString, String[] paramArrayOfString,
				File paramFile) throws IOException {
			if (paramString.length() == 0) {
				throw new IllegalArgumentException("Empty command");
			}
			StringTokenizer localStringTokenizer = new StringTokenizer(paramString);
			String[] arrayOfString = new String[localStringTokenizer.countTokens()];
			for (int i = 0; localStringTokenizer.hasMoreTokens(); ++i)
				arrayOfString[i] = localStringTokenizer.nextToken();
			return Runtime.getRuntime(). exec(arrayOfString, paramArrayOfString, paramFile);
		}
//	public String exe(String getCpuRate) {
//	try {
//		Process p=	Runtime.getRuntime().exec(getCpuRate);
//	} catch (IOException e) {
//		// TODO Auto-generated catch block
//		e.printStackTrace();
//	}
//		return null;
//	}
	 	/**
		@author attilax 锟斤拷锟桔碉拷爪锟斤拷
     	 * @throws NoDeviceEx 
		@since   p14 e_52_6
		 
		 */
	private static String getDeviceNo(String txt) throws NoDeviceEx {
		  String[] a=txt.split("\n");
		    for (String line : a) {
				if(line.endsWith("device"))
				return (getDeviceNoSingle(line));
			}
		throw new NoDeviceEx("");
	}
		/**
			@author attilax 锟斤拷锟桔碉拷爪锟斤拷
			@since   p14 e_52_y
			 
			 */
		private static String getDeviceNoSingle(String line) {
			 String[] a=line.split("\t");
			return a[0];
		}
		
//		@Deprecated
//		public static Process exec(String paramString, String[] paramArrayOfString,
//				File paramFile) throws IOException {
//			if (paramString.length() == 0) {
//				throw new IllegalArgumentException("Empty command");
//			}
//			StringTokenizer localStringTokenizer = new StringTokenizer(paramString);
//			String[] arrayOfString = new String[localStringTokenizer.countTokens()];
//			for (int i = 0; localStringTokenizer.hasMoreTokens(); ++i)
//				arrayOfString[i] = localStringTokenizer.nextToken();
//			return Runtime.getRuntime(). exec(arrayOfString, paramArrayOfString, paramFile);
//		}
	private static String echoCmdResult_asStr(InputStream cmdStream)

	{
		String r="";
		
		 
		 
		BufferedReader reader = null;
		// BufferedReader reader;
		InputStreamReader inputStreamReader;
		try {
			inputStreamReader = new InputStreamReader(cmdStream,
					"gbk");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		reader = new BufferedReader(inputStreamReader);
		String line = null;
		try {
			line = reader.readLine();
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		while (line != null) {
		//	System.out.println(line);
			r=r+"\r\n"+line;
			try {
				//int count = reader.available();   BufferedReader.readLine
				line = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (reader != null) {
			try {
				reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	 	return r;
	}
	
		
		public static String exec_EX(String cmd, List<String> li) {
	 		 
	 		   // 执锟斤拷 CMD 锟斤拷锟斤拷  
	 		   Process process = null;
	 		  String[]   b=new   String[li.size()];   
	 		  li.toArray(b);   
			try {
				 
				process =Runtime.getRuntime(). exec(b,null,null);
			} catch (IOException e) {
				 
				e.printStackTrace();
				throw new RuntimeException(e );
			}  
	 		//  process.geto
	 		   // 锟斤拷锟斤拷锟斤拷锟斤拷锟叫讹拷取锟侥憋拷  
	 		   BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));  
	 		     String r="";
	 		   // 锟斤拷锟斤拷一锟斤拷写锟斤拷锟斤拷锟斤拷指锟斤拷锟斤拷锟斤拷募锟斤拷锟斤拷锟铰凤拷锟�  
	 		//   FileWriter fw = new FileWriter(new File("C:/Users/Administrator/Desktop/CmdInfo.txt"));  
	 		     
	 		   String line = null;  
	 		     
	 		   // 循锟斤拷锟斤拷取  
	 		   try {
				while ((line = reader.readLine()) != null) {  
				    // 循锟斤拷写锟斤拷  
				  r=r+(line + "\n");  
				   }
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				throw new RuntimeException(e1 );
			}  
	 		     
	 		 
	 		   // 锟截憋拷锟斤拷锟斤拷锟�  
	 		   try {
				process.getOutputStream().close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();throw new RuntimeException(e );
			}  
	 		     
	 		   System.out.println("cmd ext finish!");  
	 		   return r;
	 		  
			
	 	} 
		
		public   String exe_depre(String  cmdArr)
		{
				return		exe(cmdArr,null,null);
		}
		public   String exe(String  cmdArr, List<String> envVAR,String dir) {
	 		 
	 		   // 执锟斤拷 CMD 锟斤拷锟斤拷  
	 		   Process process = null;
//	 		  String[]   b=new   String[cmdArr.size()];   
//	 		  cmdArr.toArray(b);   
	 		 
	 		 String[] env_a=new   String[envVAR.size()];   
	 		envVAR.toArray(env_a);
			try {
				 
				process =Runtime.getRuntime(). exec(cmdArr,env_a, new File(dir));
			} catch (IOException e) {
				 
				e.printStackTrace();
				throw new RuntimeException(e );
			}  
	 		 
			String r=echoCmdResult_outV2(process.getInputStream());
	 		     
	 		   System.out.println("cmd ext finish!");  
	 		   return r;
	 		  
			
	 	} 
	public static String exec(String cmd) {
 		try {  
 		   // 执锟斤拷 CMD 锟斤拷锟斤拷  
 		   Process process = Runtime.getRuntime().exec(cmd);  
 		//  process.geto
 		   // 锟斤拷锟斤拷锟斤拷锟斤拷锟叫讹拷取锟侥憋拷  
 		   BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));  
 		     String r="";
 		   // 锟斤拷锟斤拷一锟斤拷写锟斤拷锟斤拷锟斤拷指锟斤拷锟斤拷锟斤拷募锟斤拷锟斤拷锟铰凤拷锟�  
 		//   FileWriter fw = new FileWriter(new File("C:/Users/Administrator/Desktop/CmdInfo.txt"));  
 		     
 		   String line = null;  
 		     
 		   // 循锟斤拷锟斤拷取  
 		   while ((line = reader.readLine()) != null) {  
 		    // 循锟斤拷写锟斤拷  
 		  r=r+(line + "\n");  
 		   }  
 		     
 		 
 		   // 锟截憋拷锟斤拷锟斤拷锟�  
 		   process.getOutputStream().close();  
 		     
 		   System.out.println("cmd ext finish!");  
 		   return r;
 		  } catch (Exception e) {  
 			 ExUtil.throwEx(e);
 		  }
		return "";
		
 	}  
	@Deprecated
	public static void exec0(String cmd) {
		try {  
		   // 执锟斤拷 CMD 锟斤拷锟斤拷  
		   Process process = Runtime.getRuntime().exec(cmd);  
		     
		   // 锟斤拷锟斤拷锟斤拷锟斤拷锟叫讹拷取锟侥憋拷  
		   BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));  
		     
		   // 锟斤拷锟斤拷一锟斤拷写锟斤拷锟斤拷锟斤拷指锟斤拷锟斤拷锟斤拷募锟斤拷锟斤拷锟铰凤拷锟�  
		   FileWriter fw = new FileWriter(new File("C:/Users/Administrator/Desktop/CmdInfo.txt"));  
		     
		   String line = null;  
		     
		   // 循锟斤拷锟斤拷取  
		   while ((line = reader.readLine()) != null) {  
		    // 循锟斤拷写锟斤拷  
		    fw.write(line + "\n");  
		   }  
		     
		   // 刷锟斤拷锟斤拷锟斤拷锟�  
		   fw.flush();  
		     
		   // 锟截憋拷锟斤拷锟斤拷锟�  
		   fw.close();  
		     
		   // 锟截憋拷锟斤拷锟斤拷锟�  
		   process.getOutputStream().close();  
		     
		   System.out.println("锟斤拷锟斤拷执锟斤拷锟斤拷锟�!");  
		  } catch (Exception e) {  
		   e.printStackTrace();  
		  }
	}  
       

}
